package la.iok.hzsvn.lewin.movie.service;

import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.core.exception.ErrorCode;
import la.iok.hzsvn.lewin.movie.entity.ClueCardGroup;
import la.iok.hzsvn.lewin.movie.mapper.ClueCardGroupMapper;
import la.iok.hzsvn.lewin.movie.model.ClueCardGroupParam;
import la.iok.hzsvn.lewin.movie.model.ClueCardGroupVo;
import la.iok.hzsvn.lewin.mybatis.service.BaseServiceImpl;
import la.iok.hzsvn.share.annotations.NotNull;
import la.iok.hzsvn.share.annotations.Nullable;
import la.iok.hzsvn.share.number.NumberUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ClueCardGroupServiceImpl extends BaseServiceImpl<ClueCardGroup, ClueCardGroupMapper,Long> implements ClueCardGroupService {
    protected final MovieService movieService;
    public ClueCardGroupServiceImpl(ClueCardGroupMapper mapper, MovieService movieService) {
        super(mapper);
        this.movieService = movieService;
    }

    @Override
    public Long save(@NotNull Token token, @NotNull Long movieId, @NotNull ClueCardGroupParam param) {
        movieService.validate(token,movieId);
        String name = ErrorCode.NAME_BLANK_ERROR.assertNotBlank(param.getName());
        ClueCardGroup entity = new ClueCardGroup();
        entity.setName(name);
        if(param.getScenes() != null){
            entity.setScenes(Arrays.stream(param.getScenes()).mapToObj(String::valueOf).collect(Collectors.joining(ClueCardGroup.SCENE_SEQUENCE_SPLIT)));
        }
        entity.setMovieId(movieId);
        insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(@NotNull Token token, @NotNull Long id, @NotNull ClueCardGroupParam param) {
        String name = ErrorCode.NAME_BLANK_ERROR.assertNotBlank(param.getName());
        ClueCardGroup entity = select(id);
        ErrorCode.CLUE_GROUP_NOT_EXIST.assertNotNull(entity,id);
        movieService.validate(token,entity.getMovieId());
        entity.setName(name);
        if(param.getScenes() != null){
            entity.setScenes(Arrays.stream(param.getScenes()).mapToObj(String::valueOf).collect(Collectors.joining(ClueCardGroup.SCENE_SEQUENCE_SPLIT)));
        }else{
            entity.setScenes(null);
        }
        mapper.updateSelective(entity);
    }

    @Override
    public void delete(@NotNull Token token, @NotNull Long id) {
        ClueCardGroup entity = select(id);
        ErrorCode.CLUE_GROUP_NOT_EXIST.assertNotNull(entity,id);
        movieService.validate(token,entity.getMovieId());
        delete(id);
    }

    @Override
    public List<ClueCardGroupVo> clueCardGroups(@NotNull Token token,@NotNull Long movieId) {
        movieService.validate(token,movieId);
        List<ClueCardGroup> list = clueCardGroups(movieId);
        List<ClueCardGroupVo> vos = new ArrayList<>();
        list.forEach(item->vos.add(po2vo(item)));
        return vos;
    }

    @Override
    public List<ClueCardGroup> clueCardGroups(@Nullable Long movieId) {
        if(movieId == null){
            return Collections.emptyList();
        }
        return listByProperty("movieId",movieId);
    }

    @NotNull
    private ClueCardGroupVo po2vo(@NotNull ClueCardGroup entity){
        ClueCardGroupVo vo = new ClueCardGroupVo();
        BeanUtils.copyProperties(entity,vo);
        vo.setScenes(NumberUtils.toInts(entity.getScenes(),ClueCardGroup.SCENE_SEQUENCE_SPLIT));
        return vo;
    }
}
